<?php
$lockDir = dirname(__DIR__) . '/../install/';
$firstLockFile = $lockDir . 'first.lock';

if (file_exists($firstLockFile)) {
    header('Location: second.php');
    exit;
}

$phpVersion = PHP_VERSION;
$phpRequired = '8.3.0';
$phpPass = version_compare($phpVersion, $phpRequired, '>=');

$extensions = [
    'mysqli' => extension_loaded('mysqli')
];

$writableDirs = [
    '网站根目录' => dirname(__DIR__),
    '安装目录' => $lockDir
];

$writableCheck = [];
foreach ($writableDirs as $name => $dir) {
    $writableCheck[$name] = is_writable($dir);
}

$allPass = $phpPass && !in_array(false, $extensions) && !in_array(false, $writableCheck);

if ($_POST['submit'] ?? '' && $allPass) {
    file_put_contents($firstLockFile, date('Y-m-d H:i:s'));
    header('Location: second.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>环境检测 - QQ速刷对接系统</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/bootstrap-icons.css">
</head>
<body>
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h4 class="card-title mb-0">环境检测</h4>
                    </div>
                    <div class="card-body">
                        <div class="mb-4">
                            <h5>PHP版本检测</h5>
                            <div class="alert alert-<?= $phpPass ? 'success' : 'danger' ?>">
                                <i class="bi bi-<?= $phpPass ? 'check-circle' : 'x-circle' ?>"></i>
                                当前版本: <?= $phpVersion ?> | 要求版本: >= <?= $phpRequired ?>
                            </div>
                        </div>

                        <div class="mb-4">
                            <h5>扩展检测</h5>
                            <?php foreach ($extensions as $ext => $loaded): ?>
                                <div class="alert alert-<?= $loaded ? 'success' : 'danger' ?>">
                                    <i class="bi bi-<?= $loaded ? 'check-circle' : 'x-circle' ?>"></i>
                                    <?= $ext ?> 扩展: <?= $loaded ? '已安装' : '未安装' ?>
                                </div>
                            <?php endforeach; ?>
                        </div>

                        <div class="mb-4">
                            <h5>目录权限检测</h5>
                            <?php foreach ($writableCheck as $name => $writable): ?>
                                <div class="alert alert-<?= $writable ? 'success' : 'danger' ?>">
                                    <i class="bi bi-<?= $writable ? 'check-circle' : 'x-circle' ?>"></i>
                                    <?= $name ?>: <?= $writable ? '可写' : '不可写' ?>
                                </div>
                            <?php endforeach; ?>
                        </div>

                        <?php if ($allPass): ?>
                            <form method="post">
                                <button type="submit" name="submit" value="1" class="btn btn-success btn-lg w-100">
                                    <i class="bi bi-check-circle"></i> 环境检测通过，进入下一步
                                </button>
                            </form>
                        <?php else: ?>
                            <button type="button" class="btn btn-danger btn-lg w-100" disabled>
                                <i class="bi bi-x-circle"></i> 环境检测未通过，请解决上述问题
                            </button>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>